package com.njitt.bysj.srevice;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.*;
import com.njitt.bysj.mapper.*;
import com.njitt.bysj.model.EmpModel;
import com.njitt.bysj.query.EmpQuery;
import com.njitt.bysj.utils.AssertUtil;
import com.njitt.bysj.utils.PhoneUtil;
import com.sun.xml.internal.ws.api.model.MEP;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/19 20:07
 */
@Service
public class EmpService extends BaseService<EmpBean, Integer> {

    @Resource
    private EmpBeanMapper empMapper;
    @Resource
    private DeptBeanMapper deptMapper;
    @Resource
    private EmpRoleBeanMapper empRoleMapper;
    @Resource
    private LoginBeanMapper loginMapper;
    @Resource
    private RoleBeanMapper roleMapper;
    @Resource
    private EmpDeptBeanMapper empDeptBeanMapper;

    public Map<String, Object> selectEmpByQuery(EmpQuery empQuery) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(empQuery.getPage(), empQuery.getLimit());
        PageInfo<EmpBean> info = new PageInfo<>(empMapper.selectByParams(empQuery));
        map.put("data", info.getList());
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addEmp(EmpBean emp) {
        // 参数校验
        AssertUtil.isTrue(StringUtils.isBlank(emp.getEmpName()), "姓名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(emp.getEmpSex()), "性别不能为空");
        AssertUtil.isTrue(emp.getEmpAge() == null, "年龄不能为空");
        AssertUtil.isTrue(emp.getEmpAge() <= 0, "年龄不规范");
        AssertUtil.isTrue(StringUtils.isBlank(emp.getEmpPhone()), "联系方式不能为空");
        AssertUtil.isTrue(!PhoneUtil.isMobile(emp.getEmpPhone()), "联系方式格式错误");
        AssertUtil.isTrue(StringUtils.isBlank(emp.getAccount()), "工号不能为空");
        AssertUtil.isTrue(emp.getDeptId() == null, "部门号不能为空");
        DeptBean deptBean = deptMapper.selectByPrimaryKey(emp.getDeptId());
        AssertUtil.isTrue(deptBean == null, "部门部存在");
        LoginBean loginBean = loginMapper.queryUserByAccount(emp.getAccount());
        AssertUtil.isTrue(loginBean != null, "工号重复");
        AssertUtil.isTrue(StringUtils.isBlank(emp.getEmpEducation()), "学历不能为空");
        // 添加登陆表
        loginBean = new LoginBean();
        loginBean.setAccount(emp.getAccount());
        loginBean.setPassword("123456");
        loginBean.setStatus(0);
        loginMapper.insertHasKey(loginBean);
        AssertUtil.isTrue(loginBean.getId() == 0, "添加登录表");
        // 添加员工表
        emp.setLoginId(loginBean.getId());
        emp.setEmpEntiydate(new Date());
        emp.setEmpStatus(0);
        if (emp.getDeptId() == 1) {
            emp.setIsLeader(2);
        } else {
            emp.setIsLeader(0);
        }
        Integer empSize = empMapper.insertHasKey(emp);
        AssertUtil.isTrue(empSize == null, "添加员工表失败 ");

        // 判断角色不能为空
        AssertUtil.isTrue(StringUtils.isBlank(emp.getRoleIds()), "角色不能为空");
        // 添加员工角色表
        String[] roleIdsArray = emp.getRoleIds().split(",");
        List<EmpRoleBean> empRole = new ArrayList<>();
        for (String roleId : roleIdsArray) {
            EmpRoleBean empRoleBean = new EmpRoleBean();
            empRoleBean.setRoleId(Integer.parseInt(roleId));
            empRoleBean.setEmpId(emp.getId());
            empRoleBean.setUpdateTime(new Date());
            empRole.add(empRoleBean);
        }
        AssertUtil.isTrue(empRoleMapper.insertBatch(empRole) != empRole.size(), "添加员工角色表失败");

    }

    public EmpModel findEmpByLoginId(Integer loginId) {
        AssertUtil.isTrue(loginId == null, "这是一个异常");
        return empMapper.findEmpByLoginId(loginId);
    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateEmpByEmp(EmpBean empBean) {
        /**
         * @description: 首先判断一些属性     然后删除原始角色 添加新的角色
         * @return:  * @param empBean
         * @author:
         * @time: 2022/3/23 19:41
         */
        AssertUtil.isTrue(empBean.getId() == null, "编号错误");
        AssertUtil.isTrue(!PhoneUtil.isMobile(empBean.getEmpPhone()), "手机号码格式有误");
        AssertUtil.isTrue(!Validator.isEmail(empBean.getEmpEmail()), "邮箱格式有误");
        AssertUtil.isTrue(empBean.getDeptId() == null, "部门编号有误");
        if (!StringUtils.isBlank(empBean.getRoleIds())) {
            String[] roleIdsArray = empBean.getRoleIds().split(",");
            roleMapper.deleteEmpRoleByEmpId(empBean.getId());
            List<EmpRoleBean> empRoleBeans = new ArrayList<>();
            for (String roleId : roleIdsArray) {
                EmpRoleBean empRoleBean = new EmpRoleBean();
                empRoleBean.setEmpId(empBean.getId());
                empRoleBean.setRoleId(Integer.parseInt(roleId));
                empRoleBean.setUpdateTime(new Date());
                empRoleBeans.add(empRoleBean);
            }
            if (empBean.getIsLeader() == 1) {
                // 首先判断是否是不是组长
                EmpDeptBean empDeptBean = empDeptBeanMapper.selectByEmpAndDept(empBean.getId(), empBean.getDeptId());
                if (empDeptBean == null) {
                    // 他不是他这个部门的组长   判断部门是否改动  查他这个部门的组长是谁
                    EmpDeptBean temp = empDeptBeanMapper.selectByDept(empBean.getDeptId());
                     if (temp.getEmpId() !=0) {
                        System.out.println(temp.getEmpId());
                        EmpBean tempEmp = empMapper.selectByPrimaryKey(temp.getId());
                        tempEmp.setIsLeader(0);
                        AssertUtil.isTrue(empMapper.updateByPrimaryKeySelective(tempEmp) < 1, "更新失败");
                    }
                    temp.setEmpId(empBean.getId());
                    AssertUtil.isTrue(empDeptBeanMapper.updateByPrimaryKeySelective(temp) < 1, "更新失败2");


                } else if (empDeptBean != null) {
                    // 他是组长 判断部门
                    if (!empBean.getDeptId().equals(empDeptBean.getDeptId())) {
                        // 当前端部门和后端部门不相同时  把 这个员工调到别的部门当 负责人
                        EmpDeptBean temp = empDeptBeanMapper.selectByDept(empBean.getDeptId());
                        EmpBean empTemp = empMapper.selectByPrimaryKey(temp.getEmpId());
                        System.out.println(empTemp);
                        empTemp.setIsLeader(0);
                        AssertUtil.isTrue(empMapper.updateByPrimaryKeySelective(empTemp) < 1, "修改失败2");
                        temp.setEmpId(empBean.getId());
                        AssertUtil.isTrue(empDeptBeanMapper.updateByPrimaryKeySelective(temp) < 1, "修改失败");
                    }
                }
            } else if (empBean.getIsLeader() == 0) {
                EmpDeptBean empDeptBean = empDeptBeanMapper.selectByEmpAndDept(empBean.getId(), empBean.getDeptId());
                if (empDeptBean != null) {
                    // 原先是组长
                    if (empBean.getDeptId().equals(empDeptBean.getDeptId())) {
                        EmpDeptBean temp = empDeptBeanMapper.selectByDept(empBean.getDeptId());
                        temp.setEmpId(0);
                        AssertUtil.isTrue(empDeptBeanMapper.updateByPrimaryKeySelective(temp) < 1, "更新失败0 ");
                    } else {
                         // 原先不是组长 现在也不时组长

                    }
                }


            }


            AssertUtil.isTrue(empRoleMapper.insertBatch(empRoleBeans) != empRoleBeans.size(), "角色添加失败");
            AssertUtil.isTrue(empMapper.updateByPrimaryKeySelective(empBean) == null, "error");
        }
    }

    /**
     * @description:
     * @return: * @param null
     * @author: 员工离职
     * @time: 2022/4/7 19:18
     */
    public void deleteEmp(Integer id) {
        AssertUtil.isTrue(id != null, "离职异常");
    }

    public List<Map<String, Object>> selectDesignerEmpByRoleInRoleName(String roleName) {
        return empMapper.selectDesignerByRoleInTechnology(roleName);
    }

    public void updatePassword(Integer empId, String oldPassword, String newPassword, String againPassword) {
        AssertUtil.isTrue(empId == null || empId == 0, "修改失败");
        EmpBean empBean = empMapper.selectByPrimaryKey(empId);
        AssertUtil.isTrue(empBean == null || !empId.equals(empBean.getId()), "待更新记录不存在");
        AssertUtil.isTrue(empBean.getLoginId() == null || empBean.getLoginId() == 0, "待更新记录不存在1");
        LoginBean loginBean = loginMapper.selectByPrimaryKey(empBean.getLoginId());
        AssertUtil.isTrue(loginBean == null || StringUtils.isBlank(loginBean.getPassword()), "待更新记录不存在");
        AssertUtil.isTrue(!loginBean.getPassword().equals(oldPassword), "原始密码不正确");
        AssertUtil.isTrue(StringUtils.isBlank(oldPassword), "原始密码为空");
        AssertUtil.isTrue(StringUtils.isBlank(newPassword), "新密码为空");
        AssertUtil.isTrue(StringUtils.isBlank(againPassword), "两次密码不一致");
        AssertUtil.isTrue(oldPassword.equals(newPassword), "新旧密码不能相同");
        AssertUtil.isTrue(!newPassword.equals(againPassword), "两次密码不一致");
        loginBean.setPassword(newPassword);
        Integer integer = loginMapper.updateByPrimaryKeySelective(loginBean);
        AssertUtil.isTrue(integer == null || integer == 0, "修改密码失败");
    }


}
